﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Linq;
namespace LunchRoller.Repositories
{
    public class JsonRepository<TValue> : FileRepository< TValue>
    {
        public JsonRepository(string fileName)
            : base(fileName )
        {
        }
            
        //public override IAsyncResult BeginSaveAll(AsyncCallback callback, object AsyncState)
        //{
        //    throw new NotImplementedException();
        //}

        //public override IAsyncResult EndSaveAll(IAsyncResult asyncResult)
        //{
        //    throw new NotImplementedException();
        //}


        static DataContractJsonSerializer json = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(TValue));
        protected override void InitData(Stream stream)
        {
            
            var array = (TValue)json.ReadObject(stream);
            this.Add(array);
        }

        protected override void WriteAllToStream(Stream stream)
        {
            json.WriteObject(stream, this.First());
        }
    }
}
